/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package academia.gui;

import academia.persistencia.DBAcademia;
import academia.persistencia.IDregistro;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Renan
 */
public class TelaInstrutor extends javax.swing.JFrame {
    Connection conn=null;
    ResultSet rs =null;
    PreparedStatement ps=null;
    /**
     * Creates new form TelaInstrutor
     */
    public TelaInstrutor() {
        initComponents();
        conn=DBAcademia.ConnectDB();
        
        String sql= "SELECT * FROM Pessoa WHERE Tipo=?";
            try{
           
                ps=conn.prepareStatement(sql);
                ps.setString(1,"Cliente");
                rs=ps.executeQuery();
                
                if(rs.next()){
                    String string ="Nome: "+rs.getString("Nome")+"/ CPF: "+rs.getString("Cpf");
                    listaNomeCpf.add(string);
                    
                    
                }else{
                    JOptionPane.showMessageDialog(null,"Aluno nao encontrado","Erro",JOptionPane.ERROR_MESSAGE);
                    txtCpfAluno.setText("");

                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null,e);
            }finally{
            
                try {
                    rs.close();
                    ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
    }

  
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCriarFicha = new javax.swing.JButton();
        txtCpfAluno = new javax.swing.JTextField();
        btnConsultarFicha = new javax.swing.JButton();
        txtCpf = new javax.swing.JTextField();
        listaNomeCpf = new java.awt.List();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(191, 205, 219));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        btnCriarFicha.setText("Criar Ficha");
        btnCriarFicha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCriarFichaActionPerformed(evt);
            }
        });

        btnConsultarFicha.setText("Ficha");
        btnConsultarFicha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarFichaActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCriarFicha, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSair)
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnConsultarFicha, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(txtCpf)))
                    .addComponent(txtCpfAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(listaNomeCpf, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(listaNomeCpf, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCpfAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCriarFicha)
                    .addComponent(btnConsultarFicha)
                    .addComponent(btnSair))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCriarFichaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCriarFichaActionPerformed
        String sql= "SELECT * FROM Pessoa WHERE CPF=?";
       
        try{
           
                ps=conn.prepareStatement(sql);
                ps.setString(1,txtCpfAluno.getText());
                rs=ps.executeQuery();
                
                if(rs.next()){
                    IDregistro.setId2(rs.getInt("ID"));
                    System.out.println(IDregistro.getId2()+","+IDregistro.getId1());
                    new TelaCriarTreino().setVisible(true);
                    this.dispose();

                }else{
                    JOptionPane.showMessageDialog(null,"Aluno nao encontrado","Erro",JOptionPane.ERROR_MESSAGE);
                    txtCpfAluno.setText("");

                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null,e);
            }finally{
            
                try {
                    rs.close();
                    ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
    }//GEN-LAST:event_btnCriarFichaActionPerformed

    private void btnConsultarFichaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarFichaActionPerformed
        String sql= "SELECT * FROM Pessoa WHERE CPF=?";
       
        try{
                ps=conn.prepareStatement(sql);
                ps.setString(1,txtCpf.getText());
                rs=ps.executeQuery();
                
                if(rs.next()){
                    IDregistro.setId2(rs.getInt("ID"));
                    new TelaFichaTreino().setVisible(true);
                    this.dispose();

                }else{
                    JOptionPane.showMessageDialog(null,"Aluno nao encontrado","Erro",JOptionPane.ERROR_MESSAGE);
                    txtCpf.setText("");

                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null,e);
            }finally{
            
                try {
                    rs.close();
                    ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
    }//GEN-LAST:event_btnConsultarFichaActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        new TelaLogin().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultarFicha;
    private javax.swing.JButton btnCriarFicha;
    private javax.swing.JButton btnSair;
    private java.awt.List listaNomeCpf;
    private javax.swing.JTextField txtCpf;
    private javax.swing.JTextField txtCpfAluno;
    // End of variables declaration//GEN-END:variables

   
    
    
}
